import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/core/utils/toast_utils.dart';

import 'package:casdon/modules/login/data/repository/auth_repository.dart';
import 'package:casdon/modules/login/domain/validators/input_validator.dart';
import 'package:casdon/modules/login/providers/login_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CheckAccountUsecase {
  final AuthRepository repository;
  final InputValidator validator;
  const CheckAccountUsecase(this.repository, this.validator);

  Future<void> execute({required String userName}) async {
    if (validator.isValude(userName) == false) {
      throw "账号格式不正确";
    }
    try {
      await repository.postCheckIsAlreadyRegister(userName);
      return;
    } on BusinessException catch (e) {
      if (e.code == "10003") {
      } else {
        ToastUtil.show(e.msg);
      }

      rethrow;
    } catch (e) {
      rethrow;
    }
  }
}

// 注册
final checkAccountUsecaseProvider = Provider(
  (ref) => CheckAccountUsecase(
    ref.read(authRepositoryProvider),
    ref.read(inputValidatorProvider),
  ),
);
